/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Account;
import entities.Comment;
import entities.Complain;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Quan Ho
 */
@ManagedBean
@SessionScoped
public class CommentProgess {
 @EJB
    private biz.ComplainProgess complainProgess;
    @EJB
    private biz.CommentProgess commentProgess;
    @EJB
    private biz.LoginProgess loginProgess;
    private String cnt;
    private String msg;
    private List<Comment> lstCmt;

    public String createComment(String name, int cplId) {
        //call ejb to get account login
        Account user = loginProgess.getUser(name);
        Complain cmpx = complainProgess.getComplainById(cplId);
        Date date = new Date();
        //Validate text box
        if ("".equals(cnt)) {
            msg = "Insert content please!";
            return "";
        }
        try {
            //Create new comment
            Comment cmt = new Comment(1);
            cmt.setId(1);
            cmt.setAccountId(user);
            cmt.setComplainId(cmpx);
            cmt.setContent(cnt);
            cmt.setDate(date);
            //call ejb to create 
            commentProgess.createComment(cmt);
            msg = "Submit complain success!";
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

    public void createListCmt(int id) {
        lstCmt = commentProgess.getCommentByComplainId(id);
    }

    public List<Comment> getLstCmt() {
        return lstCmt;
    }

    public String getCnt() {
        return cnt;
    }

    public void setCnt(String cnt) {
        this.cnt = cnt;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
